//
//  STDSTransformationEffect.hpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/22.
//

#ifndef STDSTransformationEffect_hpp
#define STDSTransformationEffect_hpp

#include <stdio.h>
#include "STDSBaseEffect.hpp"
#include <glm/gtc/type_ptr.hpp>

/// 色温效果
class STDSTransformationEffect: public STDSBaseEffect {
public:
    STDSTransformationEffect() {
        mEffectType = STDSEffectTypeTransformation;
    }
    ~STDSTransformationEffect() {
//        printf("dealloc--STDSTransformationEffect\n");
    }
    void initGL() override {
        STDSBaseEffect::initGL();
        mTransformProgram = mContext->getProgram(kShaderSRCTransformmation);
    }
    
    void unInitGL() override {
        STDSBaseEffect::unInitGL();
        mTransformProgram = nullptr;
    }
public:
    
    void setAlpha(float alpha) {
        mAlpha = alpha;
    }
    
    void draw() override {
        if (mTransformProgram == nullptr) {
            return;
        }
        mTransformProgram->use();
        if (mTexture != nullptr) {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, mTexture->getId());
            mTransformProgram->setUniform(mTransformProgram->getUniform("uTexture"), 0);
        }
        mTransformProgram->setUniform(mTransformProgram->getUniform("uAlpha"), mAlpha);
        mTransformProgram->setMatrix(mTransformProgram->getUniform("uMatVex"), glm::value_ptr(mMatV));
        mTransformProgram->setMatrix(mTransformProgram->getUniform("uMatTex"), glm::value_ptr(mMatT));
        
        mAtbRect.setPosVex(mTransformProgram->getAttrib("aPos"));
        mAtbRect.setPosTex(mTransformProgram->getAttrib("aTexCoord"));
        
        mAtbRect.draw();
        mTransformProgram->unuse();
    }

private:
    float mAlpha{1.0};
    shared_ptr<STDSShaderProgram> mTransformProgram{nullptr};
};
#endif /* STDSTransformationEffect_hpp */
